<?php
/**
 * @Author:      lyt
 * @DateTime:    2016-12-07 14:03:48
 * @Description: 用于定价策略的维护
 */
class PricingStrategy extends Eloquent
{
    protected $table = "pricing_strategy";

    public static $status = array(
        "1"=>"开启",
        "0"=>"关闭",
    );

    public function type_name(){
	    return $this->hasOne('PricingColumn',"id","type");
	}

	public function create_name()
    {
        return $this->hasOne('SystemUser',"id","created_by");
    }

    public function city_name()
    {
        return $this->hasOne('CommonCity',"id","city_id");
    }
    
    /**
     * @Author:      lyt
     * @DateTime:    2016-12-09 11:32:53
     * @Description: 获取车型适合的计费规则
     *  传入车型id ,服务id
     */
    public static function getStringStrategy($car_model_id,$service_id,$city_id){
        $csi=DataCarStandardInfo::find($car_model_id);
        $temp=DB::table('pricing_strategy_service')
        ->leftJoin("pricing_strategy","pricing_strategy.id","=","pricing_strategy_service.pricing_strategy_id")->where("pricing_strategy_service.serivee_id",$service_id)->where("pricing_strategy.city_id",$city_id)->select("pricing_strategy.*")->first();
        if($temp){//如果该服务该城市存在定价规则
            $arr=$csi->toArray();
            $pc=PricingColumn::find($temp->type);
            if(isset($arr[$pc->column])){
                $val=$arr[$pc->column];
                $psd=PricingStrategydetail::where("pricing_strategy_id",$temp->id)->where("column_type_id",$val)->first();
                if($psd){
                    return array("code"=>0,"price"=>$psd->price);
                }else{
                    return array("code"=>1);
                }
            }else{
                return array("code"=>1);
            }
        }else{
            return array("code"=>1);
        }
    }
}